package com.sky.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.sky.dto.ReportQueryDTO;
import com.sky.entity.MyXlsx;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import com.sun.deploy.net.HttpResponse;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/report")
@Slf4j
@Api(value = "数据统计接口")
public class ReportContorller {
    @Autowired
    ReportService reportService;

    @GetMapping("/ordersStatistics")
    public Result ordersStatistics(ReportQueryDTO reportQueryDTO){
        OrderReportVO orderReportVO=reportService.ordersStatistics(reportQueryDTO);
        return Result.success(orderReportVO);
    }
    @GetMapping("/turnoverStatistics")
    public Result turnoverStatistics( ReportQueryDTO reportQueryDTO){
        TurnoverReportVO turnoverReportVO=reportService.turnoverStatistics(reportQueryDTO);
        return Result.success(turnoverReportVO);
    }
    @GetMapping("/userStatistics")
    public Result userStatistics( ReportQueryDTO reportQueryDTO){
        UserReportVO userReportVO=reportService.userStatistics(reportQueryDTO);
        return Result.success(userReportVO);
    }

    @GetMapping("/top10")
    public Result top10( ReportQueryDTO reportQueryDTO){
        SalesTop10ReportVO salesTop10ReportVO=reportService.top10(reportQueryDTO);
        return Result.success(salesTop10ReportVO);
    }
    @GetMapping("/export")
    public void export(HttpServletResponse response, HttpServletRequest request) throws IOException {
        try {
            List<MyXlsx> myXlsx = reportService.export();

            // 设置响应头
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");

            // 设置导出文件名
            String fileName = "店铺运营数据.xlsx";
            String userAgent = request.getHeader("User-Agent");
            if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                // IE 浏览器
                fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            } else if (userAgent.contains("Mozilla")) {
                // 非 IE 浏览器
                fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);

            // 导出 Excel
            EasyExcel.write(response.getOutputStream(), MyXlsx.class).sheet("店铺运营数据").doWrite(myXlsx);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
